<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="cat_id" label="分类ID">
        <uni-easyinput placeholder="分类ID" type="number" v-model="formData.cat_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="cat_name" label="类别名称">
        <uni-easyinput placeholder="类别名称" v-model="formData.cat_name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="cat_pid" label="父ID">
        <uni-easyinput placeholder="父ID，用于多级分类" type="number" v-model="formData.cat_pid"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="cat_level" label="分类级别">
        <uni-easyinput placeholder="分类级别" type="number" v-model="formData.cat_level"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="cat_icon" label="图标地址">
        <uni-easyinput placeholder="类别图标/图片地址" v-model="formData.cat_icon" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="children" label="下级分类">
        <uni-data-checkbox :multiple="true" v-model="formData.children"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="description" label="类别描述">
        <uni-easyinput placeholder="类别描述" v-model="formData.description" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="orderId" label="排序">
        <uni-easyinput placeholder="数字小的排前面" type="number" v-model="formData.orderId"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="state" label="是否显示">
        <switch @change="binddata('state', $event.detail.value)" :checked="formData.state"></switch>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/opendb-mall-categories.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'opendb-mall-categories';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "cat_id": null,
        "cat_name": "",
        "cat_pid": null,
        "cat_level": null,
        "cat_icon": "",
        "children": [],
        "description": "",
        "orderId": 100,
        "state": true
      }
      return {
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
